using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class PandaBasisController : ControllerBase
    {
        private readonly IPandaBasisService _basisService;

        public PandaBasisController(IPandaBasisService basisService)
        {
            _basisService = basisService;
        }

        [HttpGet]
        public async Task<ActionResult<List<PandaBasis>>> GetAllBases()
        {
            var bases = await _basisService.GetAllAsync();
            return Ok(bases);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<PandaBasis>> GetBaseById(Guid id)
        {
            var basis = await _basisService.GetByIdAsync(id);
            if (basis == null)
                return NotFound();

            return Ok(basis);
        }

        [HttpGet("location/{location}")]
        public async Task<ActionResult<List<PandaBasis>>> GetBasesByLocation(string location)
        {
            var bases = await _basisService.GetBasesByLocationAsync(location);
            return Ok(bases);
        }

        [HttpPost]
        public async Task<ActionResult<PandaBasis>> CreateBase(PandaBasis basis)
        {
            try
            {
                var createdBasis = await _basisService.CreateAsync(basis);
                return CreatedAtAction(nameof(GetBaseById), new { id = createdBasis.BaseId }, createdBasis);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPut("{id}")]
        public async Task<ActionResult<PandaBasis>> UpdateBase(Guid id, PandaBasis basis)
        {
            if (id != basis.BaseId)
                return BadRequest();

            try
            {
                var updatedBasis = await _basisService.UpdateAsync(basis);
                return Ok(updatedBasis);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpPut("{id}/image")]
        public async Task<ActionResult<PandaBasis>> UpdateBaseImage(Guid id, [FromBody] string imageUrl)
        {
            try
            {
                var updatedBasis = await _basisService.UpdateBaseImageAsync(id, imageUrl);
                return Ok(updatedBasis);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpPut("{id}/website")]
        public async Task<ActionResult<PandaBasis>> UpdateWebsiteUrl(Guid id, [FromBody] string websiteUrl)
        {
            try
            {
                var updatedBasis = await _basisService.UpdateWebsiteUrlAsync(id, websiteUrl);
                return Ok(updatedBasis);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteBase(Guid id)
        {
            try
            {
                var result = await _basisService.DeleteAsync(id);
                if (!result)
                    return NotFound();

                return NoContent();
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
} 